import { updateDestinationApi } from "@/api/waybillManagement/customerServiceWaybill";
import { message } from "ant-design-vue";

export default function OperationHook(state: any) {
  const { passData, formState, loading, formRef, emit, getOptions } = state;

  const onSubmit = () => {
    formRef.value.validate().then(() => {
      const { region, numberCode } = getOptions.value.find((t: any) => t.id === formState.countryRegionId);
      const params = {
        ...formState,
        id: passData.value.id,
        countryRegionName: region,
        countryNumberCode: numberCode
      };
      loading.value = true;
      updateDestinationApi(params)
        .then((res: any) => {
          const { code } = res;
          if (code === 200) {
            message.success("操作成功!");
            emit("submit");
          }
        })
        .finally(() => {
          loading.value = false;
        });
    });
  };
  const resetForm = () => {
    formRef.value.resetFields();
    emit("update:visible", false);
  };
  return {
    resetForm,
    onSubmit
  };
}
